# -*- coding:utf-8 -*-
import requests
import re
import base64
# import urllib2
import rsa
import json
from lxml import etree
import binascii
import urllib.request
headers={
'Host': 'login.sina.com.cn',
'Connection': 'keep-alive',
'Content-Length': '746',
'Cache-Control': 'max-age=0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Origin': 'http://weibo.com',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 UBrowser/6.0.1471.813 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded',
'Referer': 'http://weibo.com/',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8'
}
# cookies={
# 'Cookie': 'SINAGLOBAL=119.139.42.245_1486371002.852348; UOR=,auto.sina.com.cn,; vjuids=8504d524.15a1658181c.0.783a6e4c49fdc; U_TRS1=00000048.393064da.58992d17.9302da48; lxlrtst=1488181398_o; lxlrttp=1488181398; SGUID=1488186742121_2e221d29; SCF=AksYIWAZFHoWntsJxvZL0x1kTDzeqCXzTZACErJnF4Qhfnu1blxtODPjuqJeB-9IpjP2Z2ZV9hPMv20zFR8y3WM.; ULV=1489119242413:14:1:1:183.13.55.88_1489028765.305427:1487734058335; vjlast=1488186737.1489119245.10; Apache=119.139.41.207_1489543334.319570; SUB=_2AkMvlzGzdcPhrAFVmPkcz2jlZYhH-jycQlhFAn7tJhMyAhgv7ggeqSW-JmAIi3lRuFotWIYBKKX9cULDJw..; SUBP=0033WrSXqPxfM72wWs9jqgMF55529P9D9WWEgmlM4k_d6AaszAfyjQoW5JpVF02Reo2RSheEehe4'
# }
s = requests.Session()
base_url='http://login.sina.com.cn/sso/prelogin.php?entry=sso&callback=sinaSSOController.preloginCallBack&su=18219432039&rsakt=mod&client=ssologin.js(v1.4.4)'
#获取servertime，pcid，nonce，pubkey
response=s.get(base_url)
#print response.text
servertime=re.findall('"servertime":(.*?),',response.text,re.S)
pcid=re.findall('"pcid":"(.*?)"',response.text,re.S)
nonce=re.findall('"nonce":"(.*?)"',response.text,re.S)
pubkey=re.findall('"pubkey":"(.*?)"',response.text,re.S)
rsakv=re.findall('"rsakv":"(.*?)"',response.text,re.S)
print (servertime)
print (pcid)
print(nonce)
print(pubkey)
print(rsakv)
#获取su
user_name='18218432039'
# username_ = urllib.request.quote(user_name)     # html字符转义
# username = base64.encodestring(username_)[:-1]
# print(username)
su = base64.b64encode(bytes(urllib.request.quote(user_name) , encoding = 'utf-8'))
passwd='ajd123.'
# 这个值可以在prelogin得到,因为是固定值,所以写死在这里
# weibo_rsa_n = 'EB2A38568661887FA180BDDB5CABD5F21C7BFD59C090CB2D245A87AC253062882729293E5506350508E7F9AA3BB77F4333231490F915F6D63C55FE2F08A49B353F444AD3993CACC02DB784ABBB8E42A9B1BBFFFB38BE18D78E87A0E41B9B8F73A928EE0CCEE1F6739884B9777E4FE9E88A1BBE495927AC4A799B3181D6442443'
# weibo_rsa_e = 65537  # 10001对应的10进制
# message = str(servertime) + '\t' + str(nonce) + '\n' + passwd
# key = rsa.PublicKey(int(weibo_rsa_n, 16), weibo_rsa_e)
# encropy_pwd = rsa.encrypt(message, key)
# sp=binascii.b2a_hex(encropy_pwd)
rsaPublickey = int(pubkey , 16)
key = rsa.PublicKey(rsaPublickey , 65537)
message = bytes(str(servertime) + '\t' + str(nonce) + '\n' + str(passwd) , encoding = 'utf-8')
sp = binascii.b2a_hex(rsa.encrypt(message , key))
print(sp)
post_url='http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.18)'
# postdata['su']=username
# postdata['servertime']=servertime[0]
# postdata['nonce']=nonce[0]
# postdata['rsakv']=rsakv[0]
# postdata['sp']=sp
# data=json.dumps(postdata)
data='entry=weibo&gateway=1&from=&savestate=7&useticket=1&pagerefer=http%3A%2F%2Flogin.sina.com.cn%2Fsso%2Flogout.php%3Fentry%3Dminiblog%26r%3Dhttp%253A%252F%252Fweibo.com%252Flogout.php%253Fbackurl%253D%25252F&vsnf=1&su='+str(username)+'%3D&service=miniblog&servertime='+str(servertime[0].encode('utf-8'))+'nonce='+str(nonce[0].encode('utf-8'))+'&pwencode=rsa2&rsakv='+str(rsakv[0].encode('utf-8'))+'&sp='+str(sp)+'&sr=1600*900&encoding=UTF-8&prelt=82&url=http%3A%2F%2Fweibo.com%2Fajaxlogin.php%3Fframelogin%3D1%26callback%3Dparent.sinaSSOController.feedBackUrlCallBack&returntype=META'
print(data)
response=s.post(post_url,data=data,headers=headers)
print(response.text)
data=re.findall("location.replace\(\'(.*?)\'\);" , response.text)[0]
print(data)
dd=s.get(data[0],headers=headers)
print(dd.text)
# print('login success!')
# url='http://chart.weibo.com/chartv1?rank_type=3&version=v1'
# resp=requests.get(url,cookies=cookies)
# print(resp.text)